package cn.tj212.xiaoyin.opt.controller;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.common.web.JsonView;
import cn.tj212.xiaoyin.core.consts.domain.ConstsClassify;
import cn.tj212.xiaoyin.core.consts.service.IConstsClassifyService;
import cn.tj212.xiaoyin.opt.business.IPortalBusiness;
import cn.tj212.xiaoyin.opt.vo.ConstsClassifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classify")
public class ClassifyController {

    @Autowired
    private IConstsClassifyService entityService;

    @Autowired
    private IPortalBusiness portalBusiness;

    @RequestMapping(value = "/getById")
    @ResponseBody
    public String getById(Long id){
        return JsonView.render(entityService.getById(id));
    }

    @RequestMapping("/index")
    public ModelAndView classifyIndex(ConstsClassify queryEntity, TailPage<ConstsClassify> page){
        ModelAndView mv=new ModelAndView("cms/classify/classifyIndex");
        mv.addObject("curNav","classify");
        Map<String,ConstsClassifyVO> classifyMap=portalBusiness.queryAllClassifyMap();

        //所有一级分类
        List<ConstsClassifyVO> classifysList=new ArrayList<ConstsClassifyVO>();
        for (ConstsClassifyVO vo:classifyMap.values()
             ) {
            classifysList.add(vo);
        }
        mv.addObject("classifys",classifysList);

        //二级分类
        List<ConstsClassify> subClassifys=new ArrayList<ConstsClassify>();
        for (ConstsClassifyVO vo:classifyMap.values()
             ) {
            subClassifys.addAll(vo.getSubClassifyList());
        }
        mv.addObject("subClassifys",subClassifys);
        return mv;
    }

    @RequestMapping("/doMerge")
    public String doMerge(ConstsClassify entity){
        if (entity.getId()==null){
            ConstsClassify tmpEntity=entityService.getByCode(entity.getCode());
            if (tmpEntity!=null){
                return JsonView.render(1,"此编码已存在！");
            }
            entityService.createSelectivity(entity);
        }else {
            entityService.updateSelectivity(entity);
        }
        return new JsonView().toString();
    }

    @RequestMapping("/deleteLogic")
    @ResponseBody
    public String deleteLogic(ConstsClassify entity){
        entityService.deleteLogic(entity);
        return new JsonView().toString();
    }
}
